home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / ICON_8 / BENCH_FO / OPTIONS.ICN < prev    next >
Text File  |  1990-04-06  |  3KB  |  75 lines

  1. ############################################################################
  2. #
  3. #    Name:        options.icn
  4. #
  5. #    Title:        Get command-line options
  6. #
  7. #    Authors:    Robert J. Alexander, June 10, 1988
  8. #            Gregg M. Townsend, November 9, 1989
  9. #
  10. ############################################################################
  11. #  
  12. #     options(arg,optstring) -- Get command line options.
  13. #  
  14. #     This procedure analyzes the -options on the command line
  15. #  invoking an Icon program.  The inputs are:
  16. #  
  17. #       arg         the argument list as passed to the main procedure.
  18. #
  19. #       optstring   a string of allowable option letters. If a
  20. #                   letter is followed by ":" the corresponding
  21. #                   option is assumed to be followed by a string of
  22. #                   data, optionally separated from the letter by
  23. #                   space. If instead of ":" the letter is followed
  24. #                   by a "+", the parameter will converted to an
  25. #                   integer; if a ".", converted to a real.  If opt-
  26. #                   string is omitted any letter is assumed to be
  27. #                   valid and require no data.
  28. #  
  29. #     It returns a table containing the options that were specified.
  30. #  The keys are the specified option letters. The assigned values are
  31. #  the data words following the options, if any, or 1 if the option
  32. #  has no data. The table's default value is &null.
  33. #  
  34. #     If an error is detected, stop() is called with an appropriate
  35. #  error message.
  36. #
  37. #     Options may be freely interspersed with non-option arguments.
  38. #  An argument of "-" is treated as a non-option.  The special argument
  39. #  "--" terminates option processing.  Non-option arguments are returned
  40. #  in the original argument list for interpretation by the caller.
  41. #  
  42. ############################################################################
  43.  
  44. procedure options(arg,optstring)
  45.    local x,i,c,otab,flist,o,p
  46.    /optstring := string(&letters)
  47.    otab := table()
  48.    flist := []
  49.    while x := get(arg) do
  50.       x ? {
  51.          if ="-" & not pos(0) then {
  52.             if ="-" & pos(0) then break
  53.             while c := move(1) do
  54.                if i := find(c,optstring) + 1 then
  55.                   otab[c] :=
  56.                      if any(':+.',o := optstring[i]) then {
  57.                         p := "" ~== tab(0) | get(arg) |
  58.                               stop("No parameter following -",c)
  59.                         case o of {
  60.                            ":": p
  61.                            "+": integer(p) |
  62.                                  stop("-",c," needs numeric parameter")
  63.                            ".": real(p) |
  64.                                  stop("-",c," needs numeric parameter")
  65.                            }
  66.                         }
  67.                      else 1
  68.                else stop("Unrecognized option: -",c)
  69.          }
  70.          else put(flist,x)
  71.       }
  72.    while push(arg,pull(flist))
  73.    return otab
  74. end
  75.